package com.PomeloMusic.util;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF; 
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.ViewGroup;
import android.view.ViewGroup.MarginLayoutParams;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.OvershootInterpolator;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageButton;

/**
 * [YiFenClient] com.yifen.tools.PicturesEffectUtils
 * 
 * @file PicturesEffectUtils.java
 * @TODO 
 * @author Evans (2013-1-17 5:53:58)
 */
public class PicturesEffectUtils {
	private static Context context;

	private static int xOffset = 15;
	private static int yOffset = -13;

	public static void initOffset(Context context) {
		xOffset = (int) (10.667 * context.getResources().getDisplayMetrics().density);
		yOffset = -(int) (8.667 * context.getResources().getDisplayMetrics().density);
	}

	public PicturesEffectUtils(Context context) {
		super();
		// TODO Auto-generated constructor stub
		this.context = context;
	}

	/**
	 * [YiFenClient] com.yifen.tools.PicturesEffectUtils.toRoundCorner
	 * 
	 * @TODO 
	 * 
	 * @param bitmap
	 *            (RawBitmap)
	 * @param pixels
	 *            (4 corners angle)
	 * @return Bitmap
	 * 
	 * @author Evans (2013-1-17 5:54:20)
	 */
	public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {

		Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
				bitmap.getHeight(), Config.ARGB_8888);
		Canvas canvas = new Canvas(output);

		final int color = 0xff424242;
		final Paint paint = new Paint();
		final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
		final RectF rectF = new RectF(rect);
		final float roundPx = pixels;

		paint.setAntiAlias(true);
		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(color);
		canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
	
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, rect, rect, paint);
		return output;
	}

	public static Bitmap getBitMap(int id) {

		Drawable mDrawable = context.getResources().getDrawable(id);
		BitmapDrawable mBitmapDrawable = (BitmapDrawable) mDrawable;
		Bitmap mBitmap = mBitmapDrawable.getBitmap();
		return mBitmap;

	}

	/**
	 * [YiFenClient] com.yifen.tools.PicturesEffectUtils.toRoundCorner
	 * 
	 * @TODO 
	 * 
	 * @param bitmap
	 *            (RawBitmap)
	 * @param pixels
	 *            (every corners angle)
	 * @return Bitmap
	 * 
	 * @author Evans (2013-1-17 5:54:20)
	 */
	public static Bitmap toRoundCorner(Bitmap bitmap, int lp, int rp, int tp,
			int bp) {
		return bitmap;

	}

	/**
	 * [YiFenClient] com.yifen.tools.PicturesEffectUtils.getRoundCornerBitmap
	 * 
	 * @TODO 
	 * 
	 * @param context
	 *            (from Context)
	 * @param resId
	 *            (Resources ID)
	 * @return Bitmap
	 * 
	 * @author Evans (2013-1-17 5:54:20)
	 */
	public static Bitmap getRoundCornerBitmap(Context context, int resId) {

		Drawable drawable = context.getResources().getDrawable(resId);
		BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
		Bitmap bitmap = bitmapDrawable.getBitmap();
		Bitmap output = toRoundCorner(bitmap, 500);
		return output;
	}

	public static Animation getRotateAnimation(float fromDegrees,
			float toDegrees, int durationMillis) {
		RotateAnimation rotate = new RotateAnimation(fromDegrees, toDegrees,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		rotate.setDuration(durationMillis);
		rotate.setFillAfter(true);
		return rotate;
	}

	public static void startAnimationsIn(ViewGroup viewgroup, int durationMillis) {
		for (int i = 0; i < viewgroup.getChildCount(); i++) {
			ImageButton inoutimagebutton = (ImageButton) viewgroup
					.getChildAt(i);
			inoutimagebutton.setVisibility(0);
			inoutimagebutton.setClickable(true);
			inoutimagebutton.setFocusable(true);
			MarginLayoutParams mlp = (MarginLayoutParams) inoutimagebutton
					.getLayoutParams();
			Animation animation = new TranslateAnimation(mlp.rightMargin
					- xOffset, 0F, yOffset + mlp.bottomMargin, 0F);

			animation.setFillAfter(true);
			animation.setDuration(durationMillis);
			animation.setStartOffset((i * 100)
					/ (-1 + viewgroup.getChildCount()));
			animation.setInterpolator(new OvershootInterpolator(2F));
			inoutimagebutton.startAnimation(animation);

		}
	}

	public static void startAnimationsOut(ViewGroup viewgroup,
			int durationMillis) {
		for (int i = 0; i < viewgroup.getChildCount(); i++) {
			final ImageButton inoutimagebutton = (ImageButton) viewgroup
					.getChildAt(i);
			MarginLayoutParams mlp = (MarginLayoutParams) inoutimagebutton
					.getLayoutParams();
			Animation animation = new TranslateAnimation(0F, mlp.rightMargin
					- xOffset, 0F, yOffset + mlp.bottomMargin);

			animation.setFillAfter(true);
			animation.setDuration(durationMillis);
			animation.setStartOffset(((viewgroup.getChildCount() - i) * 100)
					/ (-1 + viewgroup.getChildCount()));
			animation.setAnimationListener(new Animation.AnimationListener() {
				public void onAnimationStart(Animation arg0) {
				}

				public void onAnimationRepeat(Animation arg0) {
				}

				public void onAnimationEnd(Animation arg0) {
					inoutimagebutton.setVisibility(8);
					inoutimagebutton.setClickable(false);
					inoutimagebutton.setFocusable(false);
				}
			});
			inoutimagebutton.startAnimation(animation);
		}
	}

	public static Animation getMaxAnimation(int durationMillis) {
		AnimationSet animationset = new AnimationSet(true);

		Animation maxAnimation = new ScaleAnimation(1.0f, 4.0f, 1.0f, 4.0f,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		Animation alphaAnimation = new AlphaAnimation(1, 0);

		animationset.addAnimation(maxAnimation);
		animationset.addAnimation(alphaAnimation);

		animationset.setDuration(durationMillis);
		animationset.setFillAfter(true);
		return animationset;
	}

	public static Animation getMiniAnimation(int durationMillis) {
		Animation miniAnimation = new ScaleAnimation(1.0f, 0f, 1.0f, 0f,
				Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
				0.5f);
		miniAnimation.setDuration(durationMillis);
		miniAnimation.setFillAfter(true);
		return miniAnimation;
	}

	public static Bitmap rotateBitmap(Bitmap bitmap, float degree) {
		Matrix matrix = new Matrix();
		matrix.postRotate(degree);
		/**
		   * 
		   */
		return bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
				bitmap.getHeight(), matrix, true);

	}

}